package com.yc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.entity.Cartoon;
import com.yc.mapper.CartoonMapper;
import com.yc.service.ICartoonService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * InnoDB free: 4096 kB 服务实现类
 * </p>
 *
 * @author ly
 * @since 2022-05-25
 */
@Service
public class CartoonServiceImpl extends ServiceImpl<CartoonMapper, Cartoon> implements ICartoonService {
    @Resource
    private CartoonMapper cartoonMapper;
    @Override
    public List< Cartoon > banner() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.in("ctid",4,9,10,11);
        List<Cartoon> list=cartoonMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public List< Cartoon > queryByDate(String date) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.likeRight("last_date",date);
        return cartoonMapper.selectList(queryWrapper);
    }

    @Override
    public List< Cartoon > queryByTypes(List< String > types) {
        QueryWrapper queryWrapper=new QueryWrapper();
        for ( int i = 0; i < types.size(); i++ ) {
            queryWrapper.like("ctype",types.get(i));
            if ( i+1<types.size() ){
                queryWrapper.or();
            }
        }
        queryWrapper.last("ORDER BY RAND() LIMIT 10");
        return cartoonMapper.selectList(queryWrapper);
    }

    @Override
    public List<Cartoon> querydirector(String director) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("director",director);
        return cartoonMapper.selectList(queryWrapper);
    }

    @Override
    public List<Cartoon> queryprota(String prota) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("protagonist",prota);
        return cartoonMapper.selectList(queryWrapper);
    }

    @Override
    public List<Cartoon> queryctype(String ctype) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("ctype",ctype);
        return cartoonMapper.selectList(queryWrapper);
    }

    @Override
    public List< Cartoon > queryByTypesOrderByCscore(List< String > types) {
        QueryWrapper queryWrapper=new QueryWrapper();
        for ( int i = 0; i < types.size(); i++ ) {
            queryWrapper.like("ctype",types.get(i));
            if ( i+1<types.size() ){
                queryWrapper.or();
            }
        }
        queryWrapper.last("ORDER BY cscore desc LIMIT 10");
        return cartoonMapper.selectList(queryWrapper);
    }

    @Override
    public List< Cartoon > map() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByDesc("last_date");
        return cartoonMapper.selectList(queryWrapper);
    }

    @Override
    public Integer addCartoon(Cartoon cartoon) {
        return cartoonMapper.insert(cartoon);
    }

    @Override
    public List< Cartoon > queryByCname(String cname) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("cname",cname);
        return cartoonMapper.selectList(queryWrapper);
    }

    @Override
    public Integer deleteByCtid(Integer ctid) {
        return cartoonMapper.deleteById(ctid);
    }

    @Override
    public Integer update(Cartoon cartoon) {
        return cartoonMapper.updateById(cartoon);
    }

    @Override
    public List<Cartoon> queryCname(String cname) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("cname",cname);
        return cartoonMapper.selectList(queryWrapper);
    }
}
